"""
get Words form html.
the html include a lot of a novel.
take the novel to a text file.
"""

import re, requests, os
from requests.exceptions import *
header = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}


def getHtml(url):
    """
    get a code from a html through url.
    :param url:
    :return: A str about the html.
    """
    global page
    try:
        page = requests.get(url, headers=header, timeout=3)
        page.encoding = "utf-8"
        if page.status_code is 200:
            page = page.text
        else:
            print("网页获取获取错误。")
            return 'Error'
    except ConnectionError:
        print("连接错误")
        return 'Error'
    except TimeoutError:
        print("连接超时")
        return 'Error'
    except ReadTimeout:
        print("连接超时")
        return 'Error'
    if not os.path.exists('./Support'):
        os.mkdir('./Support')
    with open("./Support/tempHTML.html", mode='w', encoding='utf8')as f:
        f.write(f"{page}")
    return page


def bedeckText(long=""):
    """ none"""
    textN, text = '', ''
    # takeout &nbsp
    listLong = long.split("&nbsp;")
    for i in listLong:
        if i is not '':
            textN = textN + i
    # out <br>
    listLong = textN.split('<br>')
    for x in listLong:
        if x is not '':
            text += x
        else:
            if text[-1:] is not '\n':
                text += '\n'
    return text


def fileWrite(text, name):
    if not os.path.exists('./novel'):
        os.mkdir('./novel')
    n = 30
    while n:
        try:
            with open(f'./novel/{name}.txt', mode='w', encoding='utf8') as f:
                f.write(f"{text}")
            break
        except OSError:
            name = name[-n:]
            n -= 1

    return 'successful', name


def main(url):
    """ The function can finish the script all of things.
    Convenient for other script calls.
    """
    html = getHtml(url)
    if html is 'Error':
        print(f"Novel HTML文件获取失败。（{url}）")
        return 'Error'
    name = re.findall(r'<span id="subject_tpc">(.*?)</span>', html)[0]
    txt = re.findall(r'<div class="f14" id="read_tpc">(.*?)</div>', html)[0]  # return a LIST
    txt = bedeckText(txt)
    result = fileWrite(txt, name)
    print(f'{result[0]} writing the file is {result[1]}.txt ')


if __name__ == '__main__':
    URL = r"http://w2.xggfgczh781.pw/pw/html_data/17/1901/3860176.html"
    main(URL)
